from fastapi import APIRouter, HTTPException, Depends
from typing import List
from sqlalchemy.orm import Session
from schema import AI_Model_schema
from controller import AI_Model_controller
from database.db_init import get_db


router = APIRouter()


@router.post("/AI_Model", response_model=AI_Model_schema.Ai_model, tags=["AI_Model"])
async def create_AI_Model(AI_Model: AI_Model_schema.Ai_model, db: Session = Depends(get_db)):
    return AI_Model_controller.create_AI_Model(db, AI_Model)


@router.get("/AI_Model/{ID}", response_model=AI_Model_schema.Ai_model, tags=["AI_Model"])
async def get_AI_Model(ID: int, db: Session = Depends(get_db)):
    db_AI_Model = AI_Model_controller.get_AI_Model(db, ID)
    if db_AI_Model is None:
        raise HTTPException(status_code=404, detail="AI_Model not found")
    return db_AI_Model


@router.get("/AI_Model", response_model=List[AI_Model_schema.Ai_model], tags=["AI_Model"])
async def get_all_AI_Model(db: Session = Depends(get_db)):
    all_db_AI_Model = AI_Model_controller.get_all_AI_Model(db)
    if all_db_AI_Model is None:
        raise HTTPException(status_code=404, detail="0 AI_Model found, empty table")
    return all_db_AI_Model


@router.put("/AI_Model/{ID}", tags=["AI_Model"])
async def update_AI_Model(ID: int, field_name: str, field_value: str, db: Session = Depends(get_db)):
    try:
        db_AI_Model = AI_Model_controller.update_AI_Model(db, ID, field_name, field_value)
        if db_AI_Model is None:
            raise HTTPException(status_code=404, detail="AI_Model not found")
        return db_AI_Model
    except ValueError as error:
        raise HTTPException(status_code=400, detail=str(error))


@router.delete("/AI_Model/{ID}", tags=["AI_Model"])
async def delete_AI_Model(ID: int, db: Session = Depends(get_db)):
    is_deleted = AI_Model_controller.delete_AI_Model(db, ID)
    if is_deleted is None:
        raise HTTPException(status_code=404, detail=f'Sample {ID} does not exist in database')
    else:
        return {'message': f'Successfully delete sample {ID}'}
